package com.example.demo01.picture_frame.frame;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import com.example.demo01.picture_frame.FrameUtils;

/**
 * <p>
 *
 * </p>
 *
 * @author DONG.WANG17@HAND-CHINA.COM
 * @since 2024-11-10 21:25
 */
public class DefaultFrame extends BaseFrame {
    private Paint paint;
    private Bitmap bitmap;
    private Bitmap scaledBitmap;
    private ViewGroup.LayoutParams params;
    // 屏幕宽高
    private final int scWidth;
    private final int scHeight;

    public DefaultFrame(Context context) {
        super(context);
        scWidth = FrameUtils.getScreenWidth(context);
        scHeight = FrameUtils.getScreenHeight(context);
        Log.d("FrameView", "Screen width: " + scWidth + ", height: " + scHeight);
        init();
    }

    private void init() {
        paint = new Paint();
        if (getLayoutParams() == null) {
            setLayoutParams(new FrameLayout.LayoutParams(0, 0));
        }
        this.params = getLayoutParams();
    }


    public void setBitmap(Bitmap bitmap) {
        this.bitmap = bitmap;
        this.scaledBitmap = scaleBitmapToFit(scWidth - 50, scHeight/2 - 100, bitmap);
        this.params.width = scWidth;
        this.params.height = scHeight/2;
        setLayoutParams(params);
        invalidate(); // 重新绘制视图
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        invalidate();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (this.bitmap != null) {
            canvas.drawColor(Color.WHITE);


            paint.setShadowLayer(50, 0, 0, Color.BLACK);



            float left = (this.params.width - this.scaledBitmap.getWidth()) / 2f;
            float top = (this.params.height - this.scaledBitmap.getHeight()) / 2f;

            canvas.drawBitmap(this.scaledBitmap, left, top, paint);

            paint.setTextSize(100);
            paint.setColor(Color.BLUE);
            paint.setShadowLayer(5, 10, 10, Color.BLACK);
            canvas.drawText("默认视图", 0, 200, paint);
        }
    }
}
